package com.bilibili.liveremind.module.controller;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bilibili.liveremind.module.entity.LiveRoom;
import com.bilibili.liveremind.module.entity.UserLive;
import com.bilibili.liveremind.module.service.ILiveRoomService;
import com.bilibili.liveremind.module.service.IUserLiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/liveRoom")
public class LiveRoomController {

    @Autowired
    private ILiveRoomService liveRoomService;
    @Autowired
    private IUserLiveService userLiveService;

    @GetMapping("/liveInfo")
    public Map<String,Object> liveInfo(String roomId){
        //获取直播间信息
        String liveInfoResult = HttpUtil.get("https://live.bilibili.com/" + roomId);
        //使用正则表达式获取直播间信息
        List<String> liveInfoList = ReUtil.findAllGroup1("<script>window.__NEPTUNE_IS_MY_WAIFU__=(.*?)</script>", liveInfoResult);
        JSONObject obj = JSONUtil.parseObj(liveInfoList.get(0));
        //直播标题
        String roomTitle = obj.getJSONObject("roomInfoRes").getJSONObject("data").getJSONObject("room_info").getStr("title");
        //直播封面
        String roomCover = obj.getJSONObject("roomInfoRes").getJSONObject("data").getJSONObject("room_info").getStr("cover");
        roomCover = roomCover.replace("http:","https:").replaceAll("\\u002F","/");
        //直播标签
        String roomTag = obj.getJSONObject("roomInfoRes").getJSONObject("data").getJSONObject("room_info").getStr("area_name");
        //用户名称-房间号 liveUname-roomId
        String anchorName = obj.getJSONObject("roomInfoRes").getJSONObject("data").getJSONObject("anchor_info").getJSONObject("base_info").getStr("uname");

        Map<String,Object> resMap = new HashMap<>();
        resMap.put("roomId",roomId);
        resMap.put("anchorName",anchorName);
        resMap.put("roomTitle",roomTitle);
        resMap.put("roomCover",roomCover);
        resMap.put("roomTag",roomTag);
        return resMap;
        //2322181
    }

    @PostMapping("/addLive")
    public String addLive(@RequestBody Map<String,Object> paramMap){
        String userId = paramMap.get("userId").toString();
        String roomId = paramMap.get("roomId").toString();
        String anchorName = paramMap.get("anchorName").toString();
        String roomTitle = paramMap.get("roomTitle").toString();
        String roomCover = paramMap.get("roomCover").toString();
        String roomTag = paramMap.get("roomTag").toString();
        LambdaQueryWrapper<LiveRoom> liveRoomLambdaQueryWrapper = new LambdaQueryWrapper<>();
        liveRoomLambdaQueryWrapper.eq(LiveRoom::getRoomId,roomId);
        LiveRoom liveRoom = liveRoomService.getOne(liveRoomLambdaQueryWrapper);
        if (null == liveRoom){
            liveRoom = new LiveRoom();
            liveRoom.setRoomId(roomId);
            liveRoom.setCreateDate(new Date());
            liveRoomService.save(liveRoom);
        }
        LambdaQueryWrapper<UserLive> userLiveLambdaQueryWrapper = new LambdaQueryWrapper<UserLive>();
        userLiveLambdaQueryWrapper.eq(UserLive::getUserId,userId);
        userLiveLambdaQueryWrapper.eq(UserLive::getLiveId,liveRoom.getId());
        userLiveLambdaQueryWrapper.eq(UserLive::getDeleteMark,0);
        int userLiveCount = userLiveService.count(userLiveLambdaQueryWrapper);
        if (0 == userLiveCount){
            UserLive userLive = new UserLive();
            userLive.setUserId(userId);
            userLive.setLiveId(liveRoom.getId());
            userLive.setRoomId(roomId);
            userLive.setAnchorName(anchorName);
            userLive.setRoomTitle(roomTitle);
            userLive.setRoomCover(roomCover);
            userLive.setRoomTag(roomTag);
            userLive.setCreateDate(new Date());
            userLiveService.save(userLive);
        }
        return "添加成功！";
    }
}
